package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.Member;
import com.example.demo.service.MemberMyBatisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 会员MyBatis控制器
 */
@RestController
@RequestMapping("/api/mybatis/members")
public class MemberMyBatisController {
    
    @Autowired
    private MemberMyBatisService memberMyBatisService;
    
    /**
     * 根据会员等级查询会员
     */
    @GetMapping("/level/{memberLevel}")
    public Result<List<Member>> findByMemberLevel(@PathVariable String memberLevel) {
        List<Member> members = memberMyBatisService.findByMemberLevel(memberLevel);
        return Result.success(members);
    }
    
    /**
     * 查询即将过生日的会员
     */
    @GetMapping("/birthday")
    public Result<List<Member>> findBirthdayMembers(@RequestParam(defaultValue = "7") Integer days) {
        List<Member> members = memberMyBatisService.findBirthdayMembers(days);
        return Result.success(members);
    }
    
    /**
     * 查询长期未消费的会员
     */
    @GetMapping("/inactive")
    public Result<List<Member>> findInactiveMembers(@RequestParam(defaultValue = "90") Integer days) {
        List<Member> members = memberMyBatisService.findInactiveMembers(days);
        return Result.success(members);
    }
    
    /**
     * 查询高价值会员
     */
    @GetMapping("/high-value")
    public Result<List<Member>> findHighValueMembers(@RequestParam(defaultValue = "1000") Double minConsumption) {
        List<Member> members = memberMyBatisService.findHighValueMembers(minConsumption);
        return Result.success(members);
    }
    
    /**
     * 统计会员总数
     */
    @GetMapping("/count")
    public Result<Long> countMembers() {
        Long count = memberMyBatisService.countMembers();
        return Result.success(count);
    }
    
    /**
     * 统计各等级会员数量
     */
    @GetMapping("/stats/by-level")
    public Result<List<Map<String, Object>>> countMembersByLevel() {
        List<Map<String, Object>> stats = memberMyBatisService.countMembersByLevel();
        return Result.success(stats);
    }
    
    /**
     * 查询会员消费排行
     */
    @GetMapping("/top-consumers")
    public Result<List<Map<String, Object>>> getTopConsumingMembers(@RequestParam(defaultValue = "10") Integer limit) {
        List<Map<String, Object>> members = memberMyBatisService.getTopConsumingMembers(limit);
        return Result.success(members);
    }
    
    /**
     * 查询新注册会员统计
     */
    @GetMapping("/stats/new-members")
    public Result<List<Map<String, Object>>> getNewMembersByMonth(@RequestParam Integer year) {
        List<Map<String, Object>> stats = memberMyBatisService.getNewMembersByMonth(year);
        return Result.success(stats);
    }
    
    /**
     * 查询会员积分统计
     */
    @GetMapping("/stats/points")
    public Result<Map<String, Object>> getPointsStatistics() {
        Map<String, Object> stats = memberMyBatisService.getPointsStatistics();
        return Result.success(stats);
    }
    
    /**
     * 根据消费金额范围查询会员
     */
    @GetMapping("/consumption-range")
    public Result<List<Member>> findByConsumptionRange(
            @RequestParam Double minAmount, 
            @RequestParam Double maxAmount) {
        List<Member> members = memberMyBatisService.findByConsumptionRange(minAmount, maxAmount);
        return Result.success(members);
    }
    
    /**
     * 查询会员活跃度统计
     */
    @GetMapping("/stats/activity")
    public Result<List<Map<String, Object>>> getMemberActivityStats() {
        List<Map<String, Object>> stats = memberMyBatisService.getMemberActivityStats();
        return Result.success(stats);
    }
    
    /**
     * 根据注册时间查询会员
     */
    @GetMapping("/register-date-range")
    public Result<List<Member>> findByRegisterDateRange(
            @RequestParam LocalDate startDate, 
            @RequestParam LocalDate endDate) {
        List<Member> members = memberMyBatisService.findByRegisterDateRange(startDate, endDate);
        return Result.success(members);
    }
}
